package sybel.pan;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import sybel.DOMPanelInterface;

/**
 *
 * @author Stefano
 */
public class Condition extends javax.swing.JPanel implements DOMPanelInterface {

    /** Creates new form Condition */
    public Condition(Node node) {
        initComponents();
        /* caso in cui il nodo condition non esiste ancora ed è stato passato
         * per parametro il padre di condition
         */
        if(node.getNodeName() != "condition"){
            father = node;
            if(father.hasChildNodes()){
                condition = (Element)father.getFirstChild();
            }
        }else{ // caso generale
            father = node.getParentNode();
            condition = (Element)node;
        }

        if(condition != null)
            initPanel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextCondition = new javax.swing.JTextField();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Condition"));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTextCondition, javax.swing.GroupLayout.PREFERRED_SIZE, 430, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTextCondition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void initPanel(){
        jTextCondition.setText(condition.getTextContent());
    }


    public Node saveData() {
        Document doc = father.getOwnerDocument();
        condition = doc.createElement("condition");
        condition.setTextContent(jTextCondition.getText());
        return (Node)condition;
    }

    public Node AppendSavedData(){
        Node temp = condition;
        Node newnode = saveData();
        if(father.hasChildNodes())
            father.replaceChild(newnode, temp);
        else
            father.appendChild(newnode);
        return father;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField jTextCondition;
    // End of variables declaration//GEN-END:variables
    private Node father;
    private Element condition = null;

}
